package com.shjy.client.activity;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.shjy.client.R;
import com.shjy.client.delegate.BaseWebviewDelegate;
import com.shjy.client.util.Const;

public class LoginActivity extends AppCompatActivity {

    private WebView mWebview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);

        // 初始化 WebView
        initWebview();

    }

    private void initWebview() {

        mWebview = (WebView) findViewById(R.id.webview);

        // settings
        mWebview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        mWebview.getSettings().setJavaScriptEnabled(true);
        mWebview.getSettings().setDefaultTextEncodingName("UTF-8");
        mWebview.getSettings().setDomStorageEnabled(true);

        mWebview.setWebViewClient(new LoginWebViewClient());
        mWebview.setWebChromeClient(new LoginWebChromeClient());

        mWebview.addJavascriptInterface(new LoginWebviewDelegate(LoginActivity.this), "delegate");

        mWebview.loadUrl(Const.URL_LOGIN);
    }

    class LoginWebViewClient extends WebViewClient {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            if (url.indexOf("index.html") != -1) {

                SharedPreferences sharedPreferences = getSharedPreferences("Login", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean("LoginSuccess", true);
                editor.commit();

                finish();
            } else {
                super.onPageStarted(view, url, favicon);
            }
        }
    }

    class LoginWebChromeClient extends WebChromeClient {

        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
            builder.setTitle(null).setMessage(message).setPositiveButton("确定", null);
            builder.setCancelable(false);
            AlertDialog dialog = builder.create();
            dialog.show();
            result.confirm();
            return true;
        }

    }

    class LoginWebviewDelegate extends BaseWebviewDelegate {

        public LoginWebviewDelegate(Context context) {
            super(context);
        }

        @JavascriptInterface
        public void goIndex() {
        }
    }
}
